import hxvid.Commands;
import hxvid.Client;
import hxvid.Server;
import hxvid.Logger;
import hxvid.Data;
import hxvid.IApplication;
import hxvid.events.ConnectionEvent;
class Application implements IApplication
{
	public var config:ApplicationConfig;
	var logger:Logger;
	public function new(server:Server, config:ApplicationConfig):Void
	{
		this.config=config;
		logger = new Logger(config.logFile);
		logger.id = "tests/Application";
		logger.info("started");
	}
	public function onConnect(client:Client, obj:ConnectionObject, ?arg:Dynamic=null)
	{
		logger.info("onConnect client:"+ client.ip);
		client.addCommands(
			[
				[ "testVoid", onTestVoid, T.Null ],
				[ "testInt", onTestInt, T.Null, T.Int ],
				[ "testString", onTestString, T.Null, T.String ],
				[ "testObject", onTestObject, T.Null, T.Object ],
				[ "testArray", onTestArray, T.Null, T.Array ],
				[ "testMixed", onTestMixed, T.Null, T.Object ],
				[ "testDate", onTestDate, T.Null, T.Date ],
				[ "testReturn", onTestReturn, T.Null, T.Object],
				[ "testMismatch", onTestMismatch, T.Null, T.Object],
				[ "testHash", onTestHash, T.Null, T.Object]
			]);
		client.addEventListener(ConnectionEvent.CONNECTED, onConnected);
		client.addEventListener(ConnectionEvent.DISCONNECTED, onDisconnected);
		client.accept();
	}
	function onConnected(event:ConnectionEvent):Void
	{
		logger.info("onConnected client:" + event.target.ip + " has received NetConnection.Connect.Success)");
		makeClientCalls(event.target);
	}
	function onDisconnected(event:ConnectionEvent):Void
	{
		var client = event.target;
		logger.info("onClientDisconnect client:" +client.ip+", id:"+client.id + ", name:" + client.name);
	}
	function makeClientCalls(client:Client):Void
	{
		client.send("testClientInt", 113);
		client.send("testClientString", "hello world");
		client.send("testClientObject", {foo:"bar"});
		client.send("testClientArray", [10, "hello world", {foo:"bar", a:["1",2]}]);
		client.send("testClientDate", Date.now());
	}
	function onTestVoid(cinf, _:Void ):Void
	{
		logger.info("onTestVoid");
	}
	function onTestInt(cinf, _:Void, v:Int):Void
	{
		logger.info("onTestInt:"+ v);
		logger.info("onTestInt:"+ (v+10));
	}
	function onTestString(cinf, _:Void, v:String):Void
	{
		logger.info("onTestString:"+ v);
		logger.info("onTestString:" + v + 10);
		logger.info("onTestString:" + v.length);
	}
	function onTestObject(cinf, _:Void, v:Dynamic /*Hash<Dynamic>*/):Void
	{
		logger.info("onTestObject:"+ Std.string(v));
		logger.info("onTestObject:"+v.name);
		//trace(v.get("name"));
	}
	function onTestArray(cinf, _:Void, v:Array<String>):Void
	{
		logger.info("onTestArray:"+ Std.string(v));
		logger.info("onTestArray:"+ v.length);
		logger.info("onTestArray:"+ v[0]);
	}
	function onTestMixed(cinf, _:Void, v:Dynamic /*Hash<Dynamic>*/):Void
	{
		logger.info("onTestMixed:"+ Std.string(v));
		logger.info("onTestMixed:"+ v.obj.age[0]+1);
		//trace(v.get("obj").get("age")[0]+1);
	}
	function onTestDate(cinf, _:Void, v:Date):Void
	{
		logger.info("onTestDate:"+ Std.string(v));
		logger.info("onTestDate:"+v.getFullYear());
	}
	function onTestReturn(cinf, _:Void, v:Dynamic /*Hash<Dynamic>*/):Void
	{
		logger.info("onTestReturn:"+ Std.string(v));
		//var sum:Int = v.get("nr1") + v.get("nr2");
		var sum:Int = v.nr1 + v.nr2;
		var client:Client = cinf.c;
		trace(sum);
		client.sendResult(cinf, sum);
	}
	//function onTestMissing(){}
	function onTestMismatch(cinf, _:Void, v:Hash<Dynamic>):Void
	{
		logger.info("onTestMismatch:"+ Std.string(v));
		var sum:Int = v.get("nr1") + v.get("nr2");
		trace(sum);
	}
	function onTestHash(cinf, _:Void, v:Hash<Int>):Void
	{
		logger.info("onTestHash:"+ Std.string(v));
		var sum:Int = v.get("one") + v.get("two");
		trace(sum);
		var client:Client = cinf.c;
		client.sendResult(cinf, sum);
	}
}